package com.cmb.lk50.sink;

import com.cmb.lk50.pojo.AbstractLog;

import java.util.concurrent.ExecutorService;

/**
 * Author       : 蒋俊钊 80256973
 * Date         : 2020/7/21
 * Copyright    (C) ChinaMerchantsBank
 */
public abstract class AsyncLogSink implements LogSink {

    private ExecutorService asyncExecutor;

    public AsyncLogSink(ExecutorService asyncExecutor) {
        this.asyncExecutor = asyncExecutor;
    }

    @Override
    final public void write(AbstractLog log) {
        if (asyncExecutor != null) {
            asyncExecutor.execute(() -> asyncWrite(log));
        } else {
            asyncWrite(log);
        }
    }

    protected abstract void asyncWrite(AbstractLog log);

}